package duoXianChen.countDownLaunch;


//import org.junit.jupiter.api.Test;
//import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


class TaskApplicationTests {
    //目标:炒菜
    //1.洗菜 5秒
    //2.买盐 3秒
    public static void main(String[] args) throws InterruptedException {
        Executor executor=Executors.newFixedThreadPool(2);
        CountDownLatch countDownLatch=new CountDownLatch(2);
        long now = System.currentTimeMillis();
        //洗菜5秒
        executor.execute(()->{
            try{
                Thread.sleep(5000);

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(countDownLatch!=null){
                    countDownLatch.countDown();
                }
            }
        });
        //买盐3秒
        executor.execute(()->{
            try{
                Thread.sleep(3000);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(countDownLatch!=null){
                    countDownLatch.countDown();
                }
            }
        });
        countDownLatch.await();
        System.out.println("可以炒菜了"+(System.currentTimeMillis()-now));
    }
}

